<?php
namespace Admin\Controller;
use Think\Controller;
class NewsController extends AdminController {
    public function index(){
        // dump($_SESSION);
        // exit;
    	$uid = $_SESSION['admin']['id'];
    	//查询用户发布过的新闻信息
    	$n_list = M('news')->select(); 
        // dump($n_list);
        // exit;
    	//拿到发布人ID
    	$id = $n_list[0]['a_id'];
    	//根据发布人ID查询发布人信息
    	// $u_list = M('admin_user')->where(array('id'=>$id))->select();
    	//拿到用户名
    	$u_name = $u_list[0]['name'];
    	// dump($u_list);
    	// exit;
        // import("@.Think.Page"); //导入分页类  
        // // 分页显示输出
        $db = M("news");
        // $where = "a_id=".$uid;
        $count = $db->count();
        $pagecount = 3;
        $page = new \Think\Page($count , $pagecount);
        // $row[] = '';
        $page->parameter = $row; //此处的row是数组，为了传递查询条件
        $page->setConfig('first');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('last','尾页');
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% 第 '.I('p',1).' 页/共 %TOTAL_PAGE% 页 ( '.$pagecount.' 条/页 共 %TOTAL_ROW% 条)');
        $show = $page->show();
        $list = $db->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$show);
    	
    	//新闻内容
    	$this->assign('n_list',$n_list);
    	//用户名
    	// $this->assign('name',$u_name);
        $this->display('News/index');

    }


    //跳转新闻添加页面
    public function add()
    {
    	$this->display('News/add');
    }

    //新闻发布数据处理
    public function addo()
    {
    	// dump($_POST);
      //   $img = $_POST['article'];
      //   echo $img;
     	// exit;
     	
        $uid = $_SESSION['admin']['id'];
        $list = M('auser')->where(array('id'=>$uid))->select();
        // dump($list);
        // exit;
        $name = $list[0]['name'];
     	$title = $_POST['title'];
        $article = $_POST['article'];
        $cover = $_POST['sxy'];
    	$upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
        dump($info);
        exit;
        //新闻图片的路径拼接
        $ilist = $info['photo1']['savepath'].$info['photo1']['savename'];
        $ilist = "/Uploads/".$ilist;

        $idata = $info['photo']['savepath'].$info['photo']['savename'];
       	// 缩略图片打开地址拼接
        $imgname = "./Public/Uploads/".$idata;
        // echo $imgname;
        // exit;
        //生成图片拼接地址,存入数据库
        $imgsname = "/Uploads/".$idata;
		$image = new \Think\Image();
        $image->open($imgname);
        $img150 = $info['photo']['savepath'].'150x150'.$info['photo']['savename'];
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
        $image->thumb(150,150)->save('./Public/Uploads/'.$img150);
        $img150 = "/Uploads/".$img150;
        $time = date('Y-m-d H:i:s');
        // exit;
        if (M('news')->add(array('img'=>$ilist,'title'=>$title,'article'=>$article,'a_id'=>$uid,'cimg'=>$img150,'time'=>$time,'cover'=>$cover,'name'=>$name)) > 0) {

        	$this->success('发布成功!!!',U('News/index'));
        }else{
        	$this->error('新闻发布失败!!!');
        	exit;
        }

    }//addo()END;



    //设为轮播图
    public function carousel()
    {
    	// dump(I());
    	// exit;
    	if (M('news')->where(I())->save(array('cover'=>'2')) >=0) {
    		$this->success('成功设为轮播图');
    	}else{
    		$this->error('设置失败');
    		exit;
    	}
    }
    //取消轮播图
	public function carouselto()
    {
    	// dump(I());
    	// exit;
    	if (M('news')->where(I())->save(array('cover'=>'1')) >=0) {
    		$this->success('成功取消轮播图');
    	}else{
    		$this->error('设置失败');
    		exit;
    	}
    }





    //删除新闻
    public function del()
    {
        // dump($_POST);
        // exit;
    	$id = $_POST['id'];
        // echo 'true';
        // exit;
    	if (M('news')->where('id='.$id)->delete() >0) {
    		echo 'true';
    	}else{
    		echo 'false';
    	}
    }

    public function n_reply()
    {
        // dump($_GET);
        // exit;
        // dump(I());
        // exit;
        $id = $_GET['id'];
        //得到用户反馈信息
        $r_list = M('reply')->where(array('n_id'=>$id))->select();
        // dump($r_list);
        // exit;
        
        //拿出新闻ID
        $n_id = $r_list[0]['n_id'];
        $u_id = $r_list[0]['u_id'];
        // 得到用户资料
        $u_list = M('user')->where(array('id'=>$u_id))->select();

        //得到新闻信息
        $n_list = M('news')->where(array('id'=>$n_id))->select();
        // dump($n_list);
        // exit;
        $this->assign('r_list',$r_list);//反馈信息
        $this->assign('u_list',$u_list);
        $this->assign('n_list',$n_list);//新闻信息
        // $this->assign('r_list',$r_list);
        $this->display();
    }




    //新闻列表分类显示
// -----------------------------------------
    // 商学院新闻
     public function snew(){

        $n_list = M('news')->where('cover=1')->select(); 
        // dump($n_list);
        // exit;
        //拿到发布人ID
        $id = $n_list[0]['a_id'];
        //根据发布人ID查询发布人信息
        // $u_list = M('admin_user')->where(array('id'=>$id))->select();
        //拿到用户名
        $u_name = $u_list[0]['name'];
        // dump($u_list);
        // exit;
        // import("@.Think.Page"); //导入分页类  
        // // 分页显示输出
        $db = M("news");
        // $where = "a_id=".$uid;
        $count = $db->where('cover=1')->count();
        $pagecount = 3;
        $page = new \Think\Page($count , $pagecount);
        // $row[] = '';
        $page->parameter = $row; //此处的row是数组，为了传递查询条件
        $page->setConfig('first');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('last','尾页');
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% 第 '.I('p',1).' 页/共 %TOTAL_PAGE% 页 ( '.$pagecount.' 条/页 共 %TOTAL_ROW% 条)');
        $show = $page->show();
        $list = $db->where('cover=1')->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$show);
        
        //新闻内容
        $this->assign('n_list',$n_list);
        $this->display('News/snew');

    }

    // 轮播图新闻
    public function lnew()
    {
        $n_list = M('news')->where('cover=2')->select(); 
        // dump($n_list);
        // exit;
        //拿到发布人ID
        $id = $n_list[0]['a_id'];
        //根据发布人ID查询发布人信息
        // $u_list = M('admin_user')->where(array('id'=>$id))->select();
        //拿到用户名
        $u_name = $u_list[0]['name'];
        // dump($u_list);
        // exit;
        // import("@.Think.Page"); //导入分页类  
        // // 分页显示输出
        $db = M("news");
        // $where = "a_id=".$uid;
        $count = $db->where('cover=2')->count();
        $pagecount = 3;
        $page = new \Think\Page($count , $pagecount);
        // $row[] = '';
        $page->parameter = $row; //此处的row是数组，为了传递查询条件
        $page->setConfig('first');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('last','尾页');
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% 第 '.I('p',1).' 页/共 %TOTAL_PAGE% 页 ( '.$pagecount.' 条/页 共 %TOTAL_ROW% 条)');
        $show = $page->show();
        $list = $db->where('cover=2')->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$show);
        
        //新闻内容
        $this->assign('n_list',$n_list);
        $this->display('News/lnew');
    }


    // 成长计划
    public function cnew()
    {
        $n_list = M('news')->where('cover=3')->select(); 
        // dump($n_list);
        // exit;
        //拿到发布人ID
        $id = $n_list[0]['a_id'];
        //根据发布人ID查询发布人信息
        // $u_list = M('admin_user')->where(array('id'=>$id))->select();
        //拿到用户名
        $u_name = $u_list[0]['name'];
        // dump($u_list);
        // exit;
        // import("@.Think.Page"); //导入分页类  
        // // 分页显示输出
        $db = M("news");
        // $where = "a_id=".$uid;
        $count = $db->where('cover=3')->count();
        $pagecount = 3;
        $page = new \Think\Page($count , $pagecount);
        // $row[] = '';
        $page->parameter = $row; //此处的row是数组，为了传递查询条件
        $page->setConfig('first');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('last','尾页');
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% 第 '.I('p',1).' 页/共 %TOTAL_PAGE% 页 ( '.$pagecount.' 条/页 共 %TOTAL_ROW% 条)');
        $show = $page->show();
        $list = $db->where('cover=3')->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$show);
        
        //新闻内容
        $this->assign('n_list',$n_list);
        $this->display('News/cnew');
    }
// ----------------------------------------- 新闻分类显示 END




    public function n_content()
    {
        // dump(I());
        // dump($_GET['id']);
        // exit;
        $n_id = $_GET['id'];
        $list = M('news')->where(array('id'=>$n_id))->select();
        // dump($list);
        // exit;
        
        $this->assign('list',$list);
        $this->display();


    }
















}






